import pathlib
from pyquery.pyquery import PyQuery
from test import read_html_file
from spider.baozimanhua import Spider
from pyquery import PyQuery

baozi_spider=Spider()

def down_chapter_html(url,file_name):
    pass


def test_parse_book():
    file_path=pathlib.Path(__file__).parent.parent/'search.html'
    html=read_html_file(file_path)
    selecotr=PyQuery(html)
    # selecotr.encoding='utf8'
    res=baozi_spider.parse_book(selecotr)
    print(res)

def test_parse_chapter():
    with open('chapter.html', encoding='utf8') as f:
        text = f.read()
        selector = PyQuery(text)
        spider = Spider('text')
        chapter = spider.parse_chapter(selector)
        pass

def log_process(process):
    print(f'Current process:{process:.2f}')

def test_spider(url):
    spider = Spider()
    spider.progress_changed_handlers.append(log_process)
    book = spider.start_parse(url)
    return book

def test_search(keywords):
    # file_path=pathlib.Path(__file__).parent.parent/'search.html'
    # html=read_html_file(file_path)
    # selector=PyQuery(html)
    # book_url_list=baozi_spider.parse_search(selector)
    # print(book_url_list)
    books=baozi_spider.search(keywords,3)
    print(len(books))

if __name__ == '__main__':
    # test_search('斗破')
    # test_parse_book()
    pass